package com.dianrui.qiyouriding.activity;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.dianrui.qiyouriding.Constant;
import com.dianrui.qiyouriding.R;

import butterknife.BindView;
import butterknife.OnClick;


@SuppressWarnings("ALL")
public class ShopScoreWebviewActicity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.webview)
    WebView couponWebview;
    @BindView(R.id.act_progressbar)
    ProgressBar mProgressBar;

    @Override
    public int getContentViewId() {
        return R.layout.webview;
    }
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    public void initView() {
        customInit(true,R.color.green);
        title.setText(getString(R.string.score_shop));
        couponWebview.post(new Runnable() {
            @Override
            public void run() {
                String showCouponUrl = Constant.shop_score_url;
                couponWebview.loadUrl(showCouponUrl);
                WebSettings webSetting = couponWebview.getSettings();
                webSetting.setJavaScriptEnabled(true);//支持js
                webSetting.setUseWideViewPort(true);
                webSetting.setLoadWithOverviewMode(true);
                webSetting.setAppCacheEnabled(false);
                webSetting.setSupportZoom(true);
                webSetting.setBuiltInZoomControls(true);
                webSetting.setDisplayZoomControls(false);
                webSetting.setDomStorageEnabled(true);
                couponWebview.setWebChromeClient(new couponChromeClient());
                couponWebview.setWebViewClient(new couponWebViewClient());
            }
        });


    }

    @Override
    public void initData() {

    }


    //自定义一个WebChromeClient
    class couponChromeClient extends WebChromeClient {
        @Override
        public void onReceivedTitle(WebView view, String titles) {
            super.onReceivedTitle(view, titles);
            if (titles != null && !isFinishing()) {

            }
        }
    }

    //自定义一个WebClient
    class couponWebViewClient extends WebViewClient {
        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            if (mProgressBar != null) {
                mProgressBar.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            if (mProgressBar != null) {
                mProgressBar.setVisibility(View.GONE);
            }
        }

        @Override
        public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
            super.onReceivedError(view, request, error);
            if (mProgressBar != null) {
                mProgressBar.setVisibility(View.GONE);
            }
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }




    @OnClick(R.id.back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && couponWebview.canGoBack()) {
            couponWebview.goBack();
            return true;
        } else {
            onBackPressed();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (couponWebview.canGoBack()) {
            couponWebview.goBack();
        }else {
            finish();
        }
    }


    @OnClick({R.id.back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                if (couponWebview.canGoBack()) {
                    couponWebview.goBack();
                }else {
                    onBackPressed();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (couponWebview != null) {
            couponWebview.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (couponWebview != null) {
            couponWebview.onPause();
        }
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        couponWebview.clearHistory();
        couponWebview.removeAllViews();
        couponWebview.destroy();
        couponWebview.clearHistory();
        couponWebview.stopLoading();
        couponWebview.getSettings().setJavaScriptEnabled(false);
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                ((ViewGroup) couponWebview.getParent()).removeView(couponWebview);
            }
        });
    }
}
